package com.bahiaapk.customize;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bahiaapk.R;
import com.bahiaapk.dto.News;
import com.raptureinvenice.webimageview.image.WebImageView;

public class NewsListAdapter extends BaseAdapter {
    
	private Context context;

    private List<News> listNews;
   
    public NewsListAdapter (Context context, List<News> listNews){
        this.context = context;
        this.listNews = listNews;
    }
    
	public int getCount() {
		// TODO Auto-generated method stub
		return  listNews.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listNews.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
        // Get actual object
		News news = listNews.get(position);
		 // on View
        LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_list_news , null);
        
        // Title Item
        TextView textTitle = (TextView)view.findViewById(R.id.title);
        if(news.getTitle()!=null)
        	textTitle.setText(news.getTitle());
        
        // Date Item
        TextView textDate = (TextView)view.findViewById(R.id.date);
        if(news.getDate()!=null)
        	textDate.setText(news.getDateFormat());

        
        if(news.getUrlImage()!=null && !"".equals(news.getUrlImage())){
        	WebImageView myImage = (WebImageView)view.findViewById(R.id.image);
        	WebImageView.setDiskCachingDefaultCacheTimeout(60 * 60* 24*30); // 1 month expiration
        	myImage.setImageWithURL(context, news.getUrlImage());
        }
        
        ImageView imageArrow = (ImageView)view.findViewById(R.id.arrow);
        if(news.getId()==null)
        	imageArrow.setVisibility(View.INVISIBLE);
        	
        
        
        return view;
	}

	
	 
	
	

}
